package Experiment1;

import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.StdRandom;
import java.lang.Math;
import java.util.Scanner;

public class PercolationStats {
    
    private int N;
    private int T;
    private double data[];


    public PercolationStats(int N,int T,String algorithms_type){
        this.N=N;
        this.T=T;

        data=new double[T];
        for(int t=0;t<T;t++){
            
            int grid[][]=new int[N+1][N+1];
            double count=0;
            

            Percolation percolation=null;

            if(algorithms_type.contentEquals("QuickFind")){
                percolation=new Percolation_QuickFind(N);
            }
            else if(algorithms_type.contentEquals("WeightedQuickUnion")){
                percolation=new Percolation_WeightedQuickUnion(N);
            }
            else {
                System.err.println("参数错误："+algorithms_type);
            }
            
            while(percolation.percolates()==false){
                int i=StdRandom.uniformInt(N)+1;
                int j=StdRandom.uniformInt(N)+1;
                if(grid[i][j]==1) continue;
                grid[i][j]=1;
                percolation.open(i, j);
                count++;
                
            }
            
            double rate=count/(N*N);
            data[t]=rate;
        }
    }

    public double mean(){
        return StdStats.mean(data);
    }

    public double stddev(){
        return StdStats.stddev(data);
    }

    public double confidenceLo(){
        double stddev=stddev();
        double mean=mean();
        return mean-(1.96*stddev/(Math.sqrt(T)));
    }

    public double confidenceHi(){
        double stddev=stddev();
        double mean=mean();
        return mean+(1.96*stddev/(Math.sqrt(T)));
    }

    public double[] GetData(){
        return data;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("N:");
        int N=sc.nextInt();
        System.out.print("T:");
        int T=sc.nextInt();

        PercolationStats ex1=new PercolationStats(N, T,"QuickFind");
        System.out.println("[QuickFind]计算结果如下：");
        System.out.println("样本均值：\t"+ex1.mean());
        System.out.println("标准差：\t"+ex1.stddev());
        //System.out.println("低置信区间：\t"+ex.confidenceLo());
        //System.out.println("高置信区间：\t"+ex.confidenceHi());
        System.out.println("置信区间：\t"+"("+ex1.confidenceLo()+", "+ex1.confidenceHi()+")");

        PercolationStats ex2=new PercolationStats(N, T,"WeightedQuickUnion");
        System.out.println("[WeightedQuickUnion]计算结果如下：");
        System.out.println("样本均值：\t"+ex2.mean());
        System.out.println("标准差：\t"+ex2.stddev());
        System.out.println("置信区间：\t"+"("+ex2.confidenceLo()+", "+ex2.confidenceHi()+")");

    }

}
